package cn.mediinfo.dianzidp.builder;

import cn.com.listentech.LEDControl;
import cn.com.listentech.ScreenInfo;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.mediinfo.dianzidp.dto.PaiBanChuangKouXX;
import cn.mediinfo.dianzidp.dto.PaiBanKSXXDto;
import cn.mediinfo.dianzidp.util.SendUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class PaiBanXXLEDBuilder{

    //    private final LEDControl LEDControl;

    // 会话句柄 对于当前连接来说 每次只需要一个会话
    public int gi_handle = 0;
    public int ii_count = 0;

    public String ls_ip;

    //todo 传过去需要㐰号
    public int ls_port = 10000;

    //controlType 型号。2是T系列，3是E，Q系列卡
    public int gi_cotrolType = 3;

    public int ii_flag_2 = 0;


    /**
     * 排版大屏
     * <p>
     * -------展示专用
     *
     * @param
     * @throws RuntimeException
     */
    public void wf_send_dp1_fp(List<PaiBanKSXXDto> dw_dangtian) throws RuntimeException {
        String ls_value;
        Integer li_ret;
        Integer li_pno;
        String ls_head;
        Integer li_i;
        Integer li_y;
        String ls_left_value;
        String ls_right_value;
        Integer li_lenth_1;
        Integer li_lenth_2;
        String ls_space1;
        String ls_space2;
        li_pno = 1;

        //1.启动会话 返回控制句柄
        gi_handle = LEDControl.StartSend();

        //2.设置通讯模式
        if (LEDControl.SetTransMode(gi_handle, 1, 0, gi_cotrolType, 1) == 0) {
            throw new RuntimeException("通讯模式设置失败!");
        }
        //=====================================================================================================
        // 设置通讯参数
        //=====================================================================================================

        if (LEDControl.SetNetworkPara(gi_handle, li_pno, ls_ip, ls_port) == 2) {
            ii_count++;
            throw new RuntimeException("设置通信参数失败");
        }
        List<PaiBanChuangKouXX> dw_xsxx = wf_send_dp11(dw_dangtian);
        ScreenInfo sc = new ScreenInfo();
        sc.screenwidth = 704;
        sc.screenheight = 160;
        sc.dbcolor = 1;
        sc.baud = 115200;
        sc.screenname = "";
        li_ret = LEDControl.SetScreenInfo(gi_handle, sc);
        
        li_ret = LEDControl.AddControl(gi_handle, li_pno, 2);
        li_ret = LEDControl.AddProgram(gi_handle, li_pno, 0);
//        ls_space = SendUtil.getKongGeSpaces(7);
        ls_head = SendUtil.getKongGeSpaces(20) + "当天排班信息";

        LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, 0, 700, 30, 128, "宋体", 20, false, false, false, ls_head);
        li_y = 30;

        //循环7次
        for (li_i = 1; li_i <= 7; li_i++) {
            if (li_i > 1) {
                li_y = li_y + 17;
            }
            ls_left_value = dw_xsxx.get(0).getXsxx1();
            ls_right_value = dw_xsxx.get(0).getXsxx2();

            if (StrUtil.isBlank(ls_right_value)) {
                ls_right_value = "";
            }
            li_lenth_1 = 48 - SendUtil.getStrLength(ls_left_value);
            li_lenth_2 = 50 - SendUtil.getStrLength(ls_right_value);
            ls_space1 = SendUtil.getKongGeSpaces(li_lenth_1);
            ls_space2 = SendUtil.getKongGeSpaces(li_lenth_2);
            ls_value = StrUtil.builder().append("  ").append(ls_left_value).append(ls_space1).append("   ").append(ls_right_value).append(ls_space2).toString();
            LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, li_y, 700, 28, 255, "新宋体", 10, false, false, false, ls_value);

            /**
             * 如果位数为 1 表示 当前大屏是最后一个了，别的都被移动走了
             */
            if (dw_xsxx.size() == 1) {
                break;
            } else {
                dw_xsxx.remove(0);
            }
        }
        li_ret = LEDControl.SendControl(gi_handle, 1, 0);
        log.error("sendControl {}", li_ret);
        li_ret = LEDControl.EndSend(gi_handle);
        log.error("EndSend {}", li_ret);
    }

    /**
     * 大屏发送方法11
     */
    public  List<PaiBanChuangKouXX> wf_send_dp11(List<PaiBanKSXXDto> dw_dangtian) {
        int ls_port = 11000;  //todo 变量值 是端口值
        int li_i = 0;         //todo 未初始化变量  0
        int li_y = 0;         //todo    0
        String ls_dangtian;
        String ls_value;
        ls_dangtian = DateUtil.formatDate(new Date());
        // 过滤数据查看当前的数据
        int li_rowdangtian = dw_dangtian.size();
        String ls_ksmc, ls_ysxm, ls_ghlb, ls_sxw, ls_syhy;
        List<PaiBanChuangKouXX> dw_xsxx = new ArrayList<>();
        for (int l_row = 0; l_row < li_rowdangtian; l_row++) {
            //=====================================================================================================
            // 具体内容显示
            //=====================================================================================================
            if (li_i >= 1) {
                li_y = li_y + 17;
            }
            //=====================================================================================================
            // 组装具体的信息
            //=====================================================================================================
            int li_lenth_ksmc;
            int li_lenth_ghlb;
            int li_lenth_ysxm;
            String ls_space_ksmc, ls_space_ghlb, ls_space_ysxm;
            PaiBanKSXXDto currentPaiBanKSXX = dw_dangtian.get(l_row);
            //科室名称
            ls_ksmc = currentPaiBanKSXX.getKeShiMC();
            li_lenth_ksmc = 15 - SendUtil.getStrLength(ls_ksmc);
            ls_space_ksmc = SendUtil.getKongGeSpaces(li_lenth_ksmc);


            //挂号类别
            ls_ghlb = currentPaiBanKSXX.getGuaHaoLB();
            li_lenth_ghlb = 12 - SendUtil.getStrLength(ls_ghlb);
            ls_space_ghlb = SendUtil.getKongGeSpaces(li_lenth_ghlb);
            //医生姓名
            ls_ysxm = currentPaiBanKSXX.getYiShengXM();
            if (StrUtil.isBlank(ls_ysxm)) {
                ls_ysxm = SendUtil.getKongGeSpaces(6);
            }
            li_lenth_ysxm = 6 - SendUtil.getStrLength(ls_ysxm);
            ls_space_ysxm = SendUtil.getKongGeSpaces(li_lenth_ysxm);

            ls_sxw = currentPaiBanKSXX.getShangXiaWu();
            ls_syhy = currentPaiBanKSXX.getShengYuHaoYuan();
//            ls_value = CharSequenceUtil.padAfter(ls_ksmc,15," ") +
//            CharSequenceUtil.padAfter(ls_sxw,9," ")+
//            CharSequenceUtil.padAfter(ls_ghlb,12," ")+
//            CharSequenceUtil.padAfter(ls_syhy,6," ");
//            ls_value = String.format("%-15s %-9s %-12s %-6s",ls_ksmc,ls_sxw,ls_ghlb,ls_syhy);
            ls_value = StrUtil.builder()
                    .append(ls_ksmc).append(ls_space_ksmc).append(ls_sxw)
                    .append(" ").append(ls_ysxm).append(ls_space_ysxm).append(" ")
                    .append(ls_ghlb).append(ls_space_ghlb).append(" ").append(ls_syhy).toString();
            ls_value = ls_value + SendUtil.getKongGeSpaces(2);


            if (l_row % 2 == 0) {
                PaiBanChuangKouXX paiBanChuangKouXX = new PaiBanChuangKouXX();
                paiBanChuangKouXX.setXsxx1(ls_value);
                dw_xsxx.add(paiBanChuangKouXX);
            } else {
                PaiBanChuangKouXX paiBanChuangKouXX = dw_xsxx.get(dw_xsxx.size() - 1);
                paiBanChuangKouXX.setXsxx2(ls_value);
            }
        }
        return dw_xsxx;
    }

    /**
     * 先翻译再说
     *
     * @param paiBanKSXXDtos
     * @throws RuntimeException
     */
    public List<PaiBanChuangKouXX> wf_send_dp1(List<PaiBanKSXXDto> paiBanKSXXDtos) throws RuntimeException {
        int ls_port = 11000;  //todo 变量值 是端口值
//        int li_i = 0;         //todo 未初始化变量  0
//        int li_y = 0;         //todo 未初始化变量  0
        String ls_space, ls_value;
        int li_ret, li_pno, li_row;
//        ScreenInfo si;

        li_pno = 1;
        if (gi_handle > 0) {
            li_ret = LEDControl.EndSend(gi_handle);
        }

        //1.启动会话
        gi_handle = LEDControl.StartSend();

        //2.设置通讯模式
        if (LEDControl.SetTransMode(gi_handle, 1, 0, gi_cotrolType, 1) == 0) {
            throw new RuntimeException("通讯模式设置失败!");
        }
        //=====================================================================================================
        // 设置通讯参数
        //=====================================================================================================

        if (LEDControl.SetNetworkPara(gi_handle, li_pno, ls_ip, ls_port) == 2) {
            ii_count++;
            throw new RuntimeException("设置通信参数失败");
        }
        //循环的次数
        int li_rowdangtian = paiBanKSXXDtos.size();
        //过滤当前时间的数据，查看有多少条数据
        //li_rowdangtian = dw_dangtian.retrieve(ls_dangtian);
        //=====================================================================================================
        // 以下是药房576屏格式 42个字节 28
        //=====================================================================================================
        String ls_ksmc, ls_ysxm, ls_ghlb, ls_sxw, ls_syhy;
        List<PaiBanChuangKouXX> dw_xsxx;
//        String head = SendUtil.getKongGeSpaces(0) + "当天门诊医生排班号源信息";
        dw_xsxx = new ArrayList<>();
        for (int l_row = 0; l_row < li_rowdangtian; l_row++) {
            int li_lenth_ksmc = 0;
            int li_lenth_ghlb = 0;
            int li_lenth_ysxm = 0;
            String ls_space_ksmc, ls_space_ghlb, ls_space_ysxm;
            PaiBanKSXXDto currentPaiBanKSXX = paiBanKSXXDtos.get(l_row);
            //科室名称
            ls_ksmc = currentPaiBanKSXX.getKeShiMC();
            li_lenth_ksmc = 15 - SendUtil.getStrLength(ls_ksmc);
            ls_space_ksmc = SendUtil.getKongGeSpaces(li_lenth_ksmc);
            //挂号类别
            ls_ghlb = currentPaiBanKSXX.getGuaHaoLB();
            li_lenth_ghlb = 12 - SendUtil.getStrLength(ls_ghlb);
            ls_space_ghlb = SendUtil.getKongGeSpaces(li_lenth_ghlb);
            //医生姓名
            ls_ysxm = currentPaiBanKSXX.getYiShengXM();
            if (StrUtil.isBlank(ls_ysxm)) {
                ls_ysxm = SendUtil.getKongGeSpaces(6);
            }
            li_lenth_ysxm = 6 - SendUtil.getStrLength(ls_ysxm);
            ls_space_ysxm = SendUtil.getKongGeSpaces(li_lenth_ysxm);

            ls_sxw = currentPaiBanKSXX.getShangXiaWu();
            ls_syhy = currentPaiBanKSXX.getShengYuHaoYuan();

            ls_value = StrUtil.builder()
                    .append(ls_ksmc).append(ls_space_ksmc).append(ls_sxw)
                    .append(" ").append(ls_ysxm).append(ls_space_ysxm).append(" ")
                    .append(ls_ghlb).append(ls_space_ghlb).append(" ").append(ls_syhy).toString();
            //美化展示信息使用
            PaiBanChuangKouXX paiBanChuangKouXX = new PaiBanChuangKouXX();
            if (l_row % 2 == 0) {
                dw_xsxx.add(new PaiBanChuangKouXX());
                paiBanChuangKouXX.setXsxx1(ls_value);
            } else {
                paiBanChuangKouXX.setXsxx2(ls_value);
            }
            dw_xsxx.add(paiBanChuangKouXX);
        }
        return dw_xsxx;
    }
    /**
     * 此方法 对获取的信息进行组装 最后展示
     *
     * @param paiBanKSXXDtos
     * @throws RuntimeException
     */
    public void wf_send_dp2_fp(List<PaiBanKSXXDto> paiBanKSXXDtos) throws RuntimeException {
        List<PaiBanChuangKouXX> paiBanChuangKouXXES = wf_send_dp12(paiBanKSXXDtos);
        String ls_ip;
        String ls_value;
        Integer li_ret;
        Integer li_pno;
        String ls_head, ls_space;
        Integer li_i;
        Integer li_y;
        String ls_left_value;
        String ls_right_value;
        Integer li_lenth_1;
        Integer li_lenth_2;
        String ls_space1;
        String ls_space2;
        String ls_tail;
        li_pno = 1;
        if (gi_handle > 0) {
            LEDControl.EndSend(gi_handle);
        }
        gi_handle = LEDControl.StartSend();
        //2.设置通讯模式
        if (LEDControl.SetTransMode(gi_handle, 1, 0, gi_cotrolType, 1) == 0) {
            throw new RuntimeException("通讯模式设置失败!");
        }

        ls_ip = "192.168.17.158";
        if (LEDControl.SetNetworkPara(gi_handle, li_pno, ls_ip, ls_port) == 2) {
            ii_count++;
            throw new RuntimeException("设置通信参数失败");
        }
        if (ii_flag_2 == 0) {
            ScreenInfo sc = new ScreenInfo();
            sc.screenwidth = 1024;
            sc.screenheight = 160;
            LEDControl.SetScreenInfo(gi_handle, sc);
            ii_flag_2 = 1;                                          //表示已经设置过屏幕
        }
        LEDControl.AddControl(gi_handle, li_pno, 2);
        LEDControl.AddProgram(gi_handle, li_pno, 0);
        ls_head = SendUtil.getKongGeSpaces(33) + "周排班信息";

        LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, 0, 1024, 30, 255, "宋体", 20, false, false, false, ls_head);
        li_y = 30;
        /**
         * 只展示 6排
         */
        for (li_i = 0; li_i < 6; li_i++) {
            if (li_i > 0) {
                li_y = li_y + 17;
            }
            ls_left_value = paiBanChuangKouXXES.get(0).getXsxx1();
            ls_right_value = paiBanChuangKouXXES.get(0).getXsxx2();
            li_lenth_1 = 60 - SendUtil.getStrLength(ls_left_value);
            li_lenth_2 = 65 - SendUtil.getStrLength(ls_right_value);
            ls_space1 = SendUtil.getKongGeSpaces(li_lenth_1);
            ls_space2 = SendUtil.getKongGeSpaces(li_lenth_2);

            ls_value = StrUtil.builder().append("    ").append(ls_left_value).append(ls_space1).append("    ").append(ls_right_value).append(ls_space2).toString();

            LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, li_y, 1024, 28, 255, "新宋体", 10, false, false, false, ls_value);
            if (paiBanChuangKouXXES.size() == 1) {
                break;
            } else {
                paiBanChuangKouXXES.remove(0);
            }
        }
        ls_tail = SendUtil.getKongGeSpaces(51) + "温馨提示:    门诊各层楼都有 挂号、收费窗口";
        LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, 132, 1024, 28, 255, "幼圆", 11, false, false, false, ls_tail);
        li_ret = LEDControl.SendControl(gi_handle, 1, 0);
        log.error("sendControl {}", li_ret);
        li_ret = LEDControl.EndSend(gi_handle);
        log.error("EndSend {}", li_ret);
    }

    /**
     * ？？？
     */
    public List<PaiBanChuangKouXX> wf_send_dp12(List<PaiBanKSXXDto> paiBanKSXXDtos) {
        String ls_rqxq, ls_ksmc, ls_ysxm, ls_ghlb, ls_sxw, ls_syhy;
        String ls_value;
        int li_lenth_ksmc;
        int li_lenth_ghlb;
        int li_lenth_ysxm;
        String ls_space_ksmc, ls_space_ghlb, ls_space_ysxm;
        int l_row, l_rowyzxx;
        l_rowyzxx = paiBanKSXXDtos.size();
        // 重新初始化窗口信息
        List<PaiBanChuangKouXX> dw_yzxsxx = new ArrayList<>();
        for (l_row = 0; l_row < l_rowyzxx; l_row++) {
            PaiBanKSXXDto paiBanKSXXDto = paiBanKSXXDtos.get(l_row);
            ls_rqxq = paiBanKSXXDto.getRiqi();
            ls_ksmc = paiBanKSXXDto.getKeShiMC();
            li_lenth_ksmc = 15 - SendUtil.getStrLength(ls_ksmc);
            ls_space_ksmc = SendUtil.getKongGeSpaces(li_lenth_ksmc);

            ls_ysxm = paiBanKSXXDto.getYiShengXM();
            if (StrUtil.isBlank(ls_ysxm)) {
                ls_ysxm = SendUtil.getKongGeSpaces(6);
            }
            li_lenth_ysxm = 6 - SendUtil.getStrLength(ls_ysxm);
            ls_space_ysxm = SendUtil.getKongGeSpaces(li_lenth_ysxm);

            ls_ghlb = paiBanKSXXDto.getGuaHaoLB();
            li_lenth_ghlb = 12 - SendUtil.getStrLength(ls_ghlb);
            ls_space_ghlb = SendUtil.getKongGeSpaces(li_lenth_ghlb);

            ls_sxw = paiBanKSXXDto.getShangXiaWu();
            ls_syhy = paiBanKSXXDto.getShengYuHaoYuan();

            ls_value = ls_rqxq + " " + ls_sxw + " " + ls_ksmc + ls_space_ksmc + " " + ls_ysxm + ls_space_ysxm + " " + ls_ghlb + ls_space_ghlb + " " + ls_syhy;
            ls_value = ls_value + SendUtil.getKongGeSpaces(2);
            if (l_row % 2 == 0) {
                PaiBanChuangKouXX paiBanChuangKouXX = new PaiBanChuangKouXX();
                paiBanChuangKouXX.setXsxx1(ls_value);
                dw_yzxsxx.add(paiBanChuangKouXX);
            } else {
                PaiBanChuangKouXX paiBanChuangKouXX = dw_yzxsxx.get(dw_yzxsxx.size() - 1);
                paiBanChuangKouXX.setXsxx2(ls_value);
            }
        }
        return dw_yzxsxx;
    }
}
